// @Name    : cai_http_request_extension
// @author  : simon
// @date    : 2025/5/21 16:20
// @desc    : 不检查网络的请求
import 'cai_http_request.dart';

extension CaiHttpRequestExtension on CaiHttpRequest {

  //静态方法
  static Future<dynamic> postRequestWithoutCheck<T>(String path,
      {Object? data,
      CancelToken? cancelToken,
      ProgressCallback? onSendProgress,
      bool? showErrorToast = true}) async {
    return await CaiHttpRequest.shared.postWithoutCheck(path,
        data: data,
        cancelToken: cancelToken,
        onSendProgress: onSendProgress,
        showErrorToast: showErrorToast);
  }

  //实例方法
  Future<dynamic> postWithoutCheck<T>(String path,
      {Object? data,
      CancelToken? cancelToken,
      ProgressCallback? onSendProgress,
      bool? showErrorToast}) async {
    return requestWithoutCheck('POST', path,
        data: data,
        cancelToken: cancelToken,
        onSendProgress: onSendProgress,
        showErrorToast: showErrorToast);
  }

  // 单个请求跳过检查（通过扩展方法）
  Future<dynamic> requestWithoutCheck<T>(String method, String path,
      {Object? data,
      Map<String, dynamic>? queryParameters,
      CancelToken? cancelToken,
      CaiRequestCancelGroup? cancelGroup,
      ProgressCallback? onSendProgress,
      bool? showErrorToast}) async {
    final original = CaiHttpRequest.shared.enableNetworkCheck;
    try {
      CaiHttpRequest.shared.enableNetworkCheck = false;
      return await request(method, path,
          data: data,
          queryParameters: queryParameters,
          cancelToken: cancelToken,
          cancelGroup: cancelGroup,
          onSendProgress: onSendProgress,
          showErrorToast: showErrorToast);
    } finally {
      CaiHttpRequest.shared.enableNetworkCheck = original;
    }
  }
}

/*
dynamic jsonObj = await CaiHttpRequestExtension.postRequestWithoutCheck("/v1/logistics/menu/list",
    data: map,
    showErrorToast: true);
 */
